package com.coda.controller;

import com.coda.common.Result;
import com.coda.entity.ProductType;
import com.coda.service.ProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/warehouse/productCategory")
@Api("商品分类相关接口")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 查询商品分类树
     */
    @GetMapping("/product-category-tree")
    @ApiOperation("查询商品分类树")
    public Result getProductTypeTree(){
        List<ProductType> productTypeList = productTypeService.getProductTypeList();
        return Result.ok(productTypeList);
    }

    /**
     * 根据分类编码查询分类方法
     */
    @GetMapping("/verify-type-code")
    @ApiOperation("根据分类编码查询分类方法")
    public Result getProductTypeByCode(String typeCode){
        ProductType productType = productTypeService.getProductTypeByCode(typeCode);
        return Result.ok(productType==null);
    }

    /**
     * 增加商品分类
     */
    @PostMapping("/type-add")
    @ApiOperation("增加商品分类")
    public Result addProductType(@RequestBody ProductType productType){
        productTypeService.addProductType(productType);
        return Result.ok("添加成功");
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping("/type-delete/{id}")
    @ApiOperation("根据id删除商品分类")
    public Result deleteProductTypeById(@PathVariable Long id){
        productTypeService.deleteProductTypeById(id);
        return Result.ok("删除成功");

    }

    /**
     * 根据id修改分类
     */
    @PutMapping("/type-update")
    @ApiOperation("根据id修改分类")
    public Result updateType(@RequestBody ProductType productType){
        productTypeService.update(productType);
        return Result.ok("修改成功");
    }

}
